const app = getApp();
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    community_id:'', //当前选中id
    type_list:[],
    community_list:[],
    page:1,
    show_more:false,
    more_msg:'下拉加载更多'
  },

  onLoad:function(options){
    let _this = this;
    _this.getTypeList(); //获取分类
    _this.getUserInfo(); //获取账户信息
  },

  //获取列表信息
  get_community_list:function(){
    let _this = this,
      community_id = _this.data.community_id,
      page = _this.data.page;
    wx.showLoading({
      title: '玩命加载中',
      show_text: '数据加载中'
    })
    app._post('api/index/get_community_list', {community_id:community_id,page:page}, function(result) {
      if(result.data.length>0){
        _this.setData({
          community_list:_this.data.community_list.concat(result.data),
          page:_this.data.page*1+1,
          show_more:true,
          more_msg:'下拉加载更多'
        })
      }else{
        _this.setData({
          show_more:false,
          more_msg:'沒有更多了',
        })
      }
      wx.hideLoading();
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    let _this = this;
    if (_this.data.show_more) {
      _this.setData({
        show_more: false
      })
      _this.get_community_list();
    }
  },

  //获取栏目信息
  getTypeList:function(){
    let _this = this;
    app._post('api/index/getCommunityTypeList', {}, function(result) {
      _this.setData({
        type_list:result.data,
        community_id:result.data[0].community_id
      })
      _this.get_community_list();
    });
  },

  //图片预览
  previewImg:function(e){
    var index = e.currentTarget.dataset.index;
    var imgarr = e.currentTarget.dataset.imgarr;
    wx.previewImage({
      current: imgarr[index],     //当前图片地址
      urls: imgarr,               //所有要预览的图片的地址集合 数组形式
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },

  //获取个人信息
  getUserInfo:function(){
    let _this = this;
    wx.showLoading({
      title: '加载中',
    })
    app._post('api/member/getUserInfo', {}, function(result) {
      wx.hideLoading({})
      if(result.code == 200){
        _this.setData({
          is_login:true,
        })
      }else{
        _this.setData({
          is_login:false,
        })
      }
    });
  },

  //关注博主
  guanzhu:function(){
    
  },

  //前往详情
  to_detail:function(e){
    let _this = this,
      gest_id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: './forum_detail?id='+gest_id
    })
  },

  //发表
  publish:function(){
  	wx.navigateTo({
      url: './publish'
    })
  },

  //顶部选择
  tabSelect(e) {
    let _this = this;
    _this.setData({
      community_id: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id-1)*60,
      page:1,
      community_list:[],
    })
    _this.get_community_list(); 
  }
});
